package 数组;


public class 有序数组 {

    public static void main(String[] args) {
        int[] arr = {-4,-1,0,3,10};

        int[] res = sortedSquares(arr);

    }
    // 考虑绝对值，相当于两个有序数组合并,我们从首尾开始比，大的插入新数组
    static int[] sortedSquares(int[] nums) {
        int[] res = new int[nums.length];
        int k = nums.length - 1;
        for(int i = 0, j = nums.length-1;i <= j;){
            if(nums[i]*nums[i]>nums[j]*nums[j]){
                res[k--] = nums[i]*nums[i];
                i++;
            }else {
                res[k--] = nums[j]*nums[j];
                j--;
            }

        }

        return res;
    }
}



